package com.yunhai.recipe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.model.entity.fms.FmsMaterialUnitEntity;
import com.yunhai.model.vo.fms.FmsMaterialUnitQuery;
import com.yunhai.model.vo.fms.FmsMaterialUnitVo;
import com.yunhai.recipe.mapper.FmsMaterialUnitMapper;
import com.yunhai.recipe.service.FmsCodeDictService;
import com.yunhai.recipe.service.FmsMaterialUnitService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@Service
public class FmsMaterialUnitServiceImpl implements FmsMaterialUnitService {

    @Resource
    private FmsMaterialUnitMapper fmsMaterialUnitMapper;
    @Resource
    private FmsCodeDictService fmsCodeDictService;
    /**
     * 获取 FmsMaterialUnit 分页列表
     */
    @Transactional
    public IPage getFmsMaterialUnitPagination(PaginationVo paginationVo, FmsMaterialUnitQuery fmsMaterialUnitQuery ) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<FmsMaterialUnitEntity> lqs = new LambdaQueryWrapper();
        lqs.orderByDesc(FmsMaterialUnitEntity::getCreateTime);
        lqs.eq(fmsMaterialUnitQuery.getEnabled()!=null, FmsMaterialUnitEntity::getEnabled, fmsMaterialUnitQuery.getEnabled());
        lqs.likeRight(fmsMaterialUnitQuery.getNumber() !=null, FmsMaterialUnitEntity::getNumber, fmsMaterialUnitQuery.getNumber());
        lqs.likeRight(fmsMaterialUnitQuery.getName() !=null, FmsMaterialUnitEntity::getName, fmsMaterialUnitQuery.getName());

        fmsMaterialUnitMapper.selectPage(page, lqs);

        List<FmsMaterialUnitEntity> record = page.getRecords();
        if( !ObjectUtil.isNotEmptyList(record)){
            return page;
        }
        return page;
    }


    /**
     * 获取 FmsMaterialUnit 所有数据
     *
    */
    @Transactional
    public List<FmsMaterialUnitEntity> getFmsMaterialUnitAllList( FmsMaterialUnitQuery fmsMaterialUnitQuery ) throws Exception {

        LambdaQueryWrapper<FmsMaterialUnitEntity> lqs = new LambdaQueryWrapper();
        lqs.eq(FmsMaterialUnitEntity::getEnabled, 0);
        List<FmsMaterialUnitEntity> list = fmsMaterialUnitMapper.selectList(lqs);
        return list;
    }


    /**
     * 获取 FmsMaterialUnit 详情
    */
    @Transactional
    public FmsMaterialUnitEntity getFmsMaterialUnitDetail( Long id) throws Exception {

        FmsMaterialUnitEntity fmsMaterialUnit = fmsMaterialUnitMapper.selectById(id);

        return fmsMaterialUnit;
    }


    /**
     * 新增 FmsMaterialUnit
     * */
    @Transactional
    public void addFmsMaterialUnit(FmsMaterialUnitVo fmsMaterialUnitVo) throws Exception {

        FmsMaterialUnitEntity fmsMaterialUnit = new FmsMaterialUnitEntity();
        ReflectionCopy.copyProperties( fmsMaterialUnit, fmsMaterialUnitVo );
        fmsMaterialUnitMapper.insert(fmsMaterialUnit);

        fmsCodeDictService.updateById(3L, fmsMaterialUnitVo.getNumber());

        return;
    }


    /**
     * 更新 FmsMaterialUnit
     * */
    @Transactional
    public void updateFmsMaterialUnit( FmsMaterialUnitVo fmsMaterialUnitVo) throws Exception {

        if( fmsMaterialUnitVo.getId() == null ){
            throw GlobalException.gen(10021, "id不存在");
        }
        FmsMaterialUnitEntity fmsMaterialUnit = new FmsMaterialUnitEntity();
        ReflectionCopy.copyProperties( fmsMaterialUnit, fmsMaterialUnitVo );
        fmsMaterialUnitMapper.updateById(fmsMaterialUnit);
        return;
    }

    /**
     * 删除 FmsMaterialUnit
     * */
    @Transactional
    public void batchRemoveFmsMaterialUnit( List<Long> ids ) throws Exception {


        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        fmsMaterialUnitMapper.deleteBatchIds(ids);
    }

    @Override
    public List<FmsMaterialUnitEntity> getUnitListByIds(Set<Long> unitIds) throws Exception {
        return fmsMaterialUnitMapper.selectBatchIds(unitIds);
    }

}

      